
let _request=null;
let _curIndex=0;
let _floats=new Float32Array(4);
let _objects=[];
let _placehold=new Uint8Array(4);//0:空位,1:数值记录,2:其它记录
const _increaseSize=()=> {
    let size = _placehold.length;
    let tmp = new Float32Array(size * 2);
    for (let i = 0; i < _floats.length; i++) {
        tmp[i] = _floats[i]
    }
    _floats = tmp;

    let tmp1 = new Uint8Array(size * 2);
    for (let i = 0; i < _placehold.length; i++) {
        tmp1[i] = _placehold[i];
    }
    _placehold = tmp1;
};
const _test=(interval=1000,totalTime=10000)=> {
    let tikTok = 0;
    let start = 0;
    let timer = 0;
    let ani = () => {
        if (start === 0) start = performance.now();
        timer = performance.now() - start;
        if (timer > tikTok * interval) {
            tikTok++;
            console.log(_placehold.length);
        }
        if (timer > totalTime) {
            cancelAnimationFrame(_request);
            _request = null;
            console.log('pointer test over!');
            return;
        }
        _request = requestAnimationFrame(ani);
    };
    ani();
};
const _dispose=()=> {
    _request = null;
    _curIndex = null;
    _floats = new Float32Array(4);
    _objects = [];
    _placehold = new Uint8Array(4);
};
const _free=(i,dispose=false)=> {
    if (_placehold[i]) _placehold[i] = 0;
    if(dispose){
        if(_floats[i])_floats[i]=0;
        if(_objects[i])_objects[i]=null;
    }
};
const _init=(v,isNum=true)=>{
    if (_placehold[_placehold.length - 2] > 0) _increaseSize();
    for (let i = 0; i < _placehold.length * 2; i++) {
        if (_placehold[i] < 1) {
            if (isNum) {
                _floats[i] = v;
                _placehold[i] = 1;
            } else {
                _objects[i] = v;
                _placehold[i] = 2;
            }
            _curIndex = i;
            break;
        }
    }
    return _curIndex;
};

class pointer{}

/**
 * 变量存取区
 * P.init(Number Or Object,bool): 新建一个存值占位，返回位置索引
 * P[index]: 获取位置index处记录的值
 * P[index]=value: 对位置index进行赋值
 * P.free(index): 释放位置index的占位
 * P.dispose(): 重置所有占位及记录的值和长度
 * P.test(interval,totalTime): 测试是否存在未及时释放的占位（打印信息间隔，测试时长）
 */
let Pointer=new Proxy(pointer,{
    set:(target,key,value)=> {
        if (!_placehold[key]) console.warn('has not init key ' + key + ' yet!');
        if (_placehold[key] === 1) {
            _floats[key] = value;
        } else if (_placehold[key] === 2) {
            _objects[key] = value;
        }
        return true;
    },
    get:(target,key)=>{
        if(!_placehold[key]){
            switch (key) {
                case 'init':
                    return _init;
                case 'free':
                    return _free;
                case 'test':
                    return _test;
                case 'dispose':
                    return _dispose;
                default:
                    console.warn('has not init key [' + key + '] yet!');
            }
        }else{
            if(_placehold[key]===1){
                return _floats[key];
            }else if(_placehold[key]===2){
                return _objects[key];
            }
        }
    }
});

export default Pointer;

